<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Servlet：Java Web 开发的核心技术</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 20px;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
            position: relative;
        }
        
        .code-block::before {
            content: attr(data-lang);
            position: absolute;
            top: 8px;
            right: 12px;
            font-size: 12px;
            color: #888;
            text-transform: uppercase;
        }
        
        .feature-icon {
            width: 48px;
            height: 48px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 8px;
            top: 8px;
            width: 16px;
            height: 16px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 24px;
            width: 2px;
            height: calc(100% + 14px);
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fade-in">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">深入理解 Servlet</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">Java Web 开发的核心技术</p>
            <div class="flex justify-center space-x-6 text-sm">
                <div class="flex items-center">
                    <i class="fas fa-server mr-2"></i>
                    <span>服务器端编程</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-code mr-2"></i>
                    <span>Java EE 标准</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-globe mr-2"></i>
                    <span>Web 应用开发</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        
        <!-- What is Servlet -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">什么是 Servlet</h2>
            <div class="prose prose-lg max-w-none">
                <p class="text-gray-700 leading-relaxed mb-6">
                    <span class="drop-cap">S</span>ervlet 是 Java 平台上的一种用于开发 Web 应用程序的组件。它是基于 Java 技术的服务器端程序，主要用于处理客户端发起的 HTTP 请求并生成相应的 HTTP 响应。
                </p>
                
                <div class="bg-purple-50 rounded-lg p-6 mb-6">
                    <p class="text-gray-700">
                        Servlet 是一种基于 Java 的 API，它运行在支持 Servlet 规范的 Web 服务器上，如 Apache Tomcat、Jetty 等。Servlet 通常被用于创建动态的、可交互的 Web 应用程序，可以处理各种类型的 HTTP 请求，包括 GET、POST、PUT、DELETE 等。
                    </p>
                </div>

                <h3 class="text-2xl font-semibold mb-4 text-gray-800">Servlet 的主要特点和优势</h3>
                
                <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <div class="bg-white rounded-lg p-6 shadow-md hover:shadow-lg transition-shadow">
                        <div class="feature-icon">
                            <i class="fas fa-globe-americas"></i>
                        </div>
                        <h4 class="font-semibold text-lg mb-2">跨平台性</h4>
                        <p class="text-gray-600">由于 Java 语言的跨平台特性，Servlet 可以在各种操作系统上运行，并且与服务器无关。</p>
                    </div>
                    
                    <div class="bg-white rounded-lg p-6 shadow-md hover:shadow-lg transition-shadow">
                        <div class="feature-icon">
                            <i class="fas fa-puzzle-piece"></i>
                        </div>
                        <h4 class="font-semibold text-lg mb-2">灵活性</h4>
                        <p class="text-gray-600">Servlet 可以与各种服务器端技术和框架集成，如 JSP、Spring 等，可以灵活地处理各种类型的业务逻辑。</p>
                    </div>
                    
                    <div class="bg-white rounded-lg p-6 shadow-md hover:shadow-lg transition-shadow">
                        <div class="feature-icon">
                            <i class="fas fa-shield-alt"></i>
                        </div>
                        <h4 class="font-semibold text-lg mb-2">安全性</h4>
                        <p class="text-gray-600">Servlet 提供了安全性机制，可以对请求进行验证和授权，保护 Web 应用程序的安全。</p>
                    </div>
                    
                    <div class="bg-white rounded-lg p-6 shadow-md hover:shadow-lg transition-shadow">
                        <div class="feature-icon">
                            <i class="fas fa-rocket"></i>
                        </div>
                        <h4 class="font-semibold text-lg mb-2">高性能</h4>
                        <p class="text-gray-600">Servlet 在处理 HTTP 请求和响应时具有高效性能，能够处理大量的并发请求。</p>
                    </div>
                    
                    <div class="bg-white rounded-lg p-6 shadow-md hover:shadow-lg transition-shadow">
                        <div class="feature-icon">
                            <i class="fas fa-expand-arrows-alt"></i>
                        </div>
                        <h4 class="font-semibold text-lg mb-2">可扩展性</h4>
                        <p class="text-gray-600">Servlet 提供了丰富的 API 和扩展机制，可以方便地编写复杂的 Web 应用程序，并且易于维护和扩展。</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Creating Servlet -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">创建 Servlet</h2>
            
            <div class="timeline-container">
                <div class="timeline-item">
                    <h3 class="font-semibold text-lg mb-2">1. 创建一个 Java Web 项目</h3>
                    <p class="text-gray-600">在 IDE 中创建一个新的 Java Web 项目，比如使用 Eclipse 或 IntelliJ IDEA 等。选择创建一个 Dynamic Web Project。</p>
                </div>
                
                <div class="timeline-item">
                    <h3 class="font-semibold text-lg mb-2">2. 编写 Servlet 类</h3>
                    <p class="text-gray-600 mb-4">在项目中创建一个 Java 类，命名为 HelloWorldServlet，并让它继承自 javax.servlet.http.HttpServlet 类。</p>
                    <div class="code-block" data-lang="java">
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("text/html");
        response.getWriter().println("&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello, World!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;");
    }
}
                    </div>
                </div>
                
                <div class="timeline-item">
                    <h3 class="font-semibold text-lg mb-2">3. 配置 Servlet 映射</h3>
                    <p class="text-gray-600 mb-4">在 web.xml 文件中配置 Servlet 的映射关系，指定 Servlet 的 URL 模式以及对应的 Servlet 类。</p>
                    <div class="code-block" data-lang="xml">
&lt;web-app&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;HelloWorldServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.example.HelloWorldServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;HelloWorldServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
&lt;/web-app&gt;
                    </div>
                </div>
                
                <div class="timeline-item">
                    <h3 class="font-semibold text-lg mb-2">4. 部署项目到 Servlet 容器</h3>
                    <p class="text-gray-600">将项目打包成 WAR 文件，或者直接复制项目文件到 Servlet 容器的 webapps 目录中。</p>
                </div>
                
                <div class="timeline-item">
                    <h3 class="font-semibold text-lg mb-2">5. 启动 Servlet 容器</h3>
                    <p class="text-gray-600">启动已安装的 Servlet 容器，比如运行 Tomcat 的启动脚本。</p>
                </div>
                
                <div class="timeline-item">
                    <h3 class="font-semibold text-lg mb-2">6. 访问 Servlet 应用程序</h3>
                    <p class="text-gray-600">在浏览